package com.quick.calcu.controller;

import com.quick.calcu.entity.AnswerRecord;
import com.quick.calcu.entity.ClassRecord;
import com.quick.calcu.entity.WrongQuestion;
import com.quick.calcu.service.AnswerRecordService;
import com.quick.calcu.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/answer-records")
public class AnswerRecordController {

    @Autowired
    private AnswerRecordService answerRecordService;

    @Autowired
    private StudentService studentService;

    /**
     * 创建答题记录
     */
    @PostMapping
    public Map<String, Object> createRecord(@RequestBody AnswerRecord record) {
        Map<String, Object> result = new HashMap<>();
        try {
            // 检查学生是否存在
            if (record.getStudentId() == null) {
                result.put("code", 400);
                result.put("message", "用户ID不能为空");
                return result;
            }

            long s = studentService.getByUserId(record.getStudentId()).getStudentId();
            record.setStudentId(s);
            if (record.getStudentId() == null) {
                result.put("code", 400);
                result.put("message", "学生ID不能为空");
                return result;
            }

            // 保存记录
            boolean success = answerRecordService.save(record);

            result.put("code", 200);
            result.put("message", success ? "创建成功" : "创建失败");
            result.put("data", record);
        } catch (Exception e) {
            result.put("code", 400);
            result.put("message", "服务器内部错误");
            e.printStackTrace();
        }
        return result;
    }


    /**
     * 根据学生ID获取答题记录
     */
    @GetMapping ("/student/{studentId}")
    public ResponseEntity<Map<String, Object>> getRecordByStudentId(@PathVariable Long studentId) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<AnswerRecord> questions = answerRecordService.getRecordByStudentId(studentId);
            result.put("code", "200");
            result.put("message", "查询成功");
            result.put("data", questions);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询失败：" + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
        }
    }

    /**
     * 根据班级id和日期获取答题记录
     */
    @GetMapping("/getRecordByClassIdAndTime")
    public ResponseEntity<Map<String, Object>> getRecordByClassIdAndTime(@RequestParam Long classId, @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate time) throws ParseException {
        Map<String, Object> result = new HashMap<>();
        try {
            ClassRecord classRecord = answerRecordService.getRecordByClassId(classId,time);
            result.put("code", "200");
            result.put("message", "查询成功");
            result.put("data", classRecord);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询失败：" + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
        }
    }

    /**
     * 根据学生id和时间获取答题纪录
     */
    @GetMapping("/getRecordByStudentIdAndTime")
    public ResponseEntity<Map<String, Object>> getRecordByStudentIdAndTime(@RequestParam Long studentId,@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate time) throws ParseException {
        Map<String, Object> result = new HashMap<>();

        try {
            List<AnswerRecord> records = answerRecordService.getRecordByStudentIdAndTime(studentId, time);
            if (records != null) {
                result.put("success", true);
                result.put("data", records);
                return ResponseEntity.ok(result);
            } else {
                result.put("success", false);
                result.put("message", "未找到对应答题记录");
                return ResponseEntity.notFound().build();
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询失败：" + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
        }
    }
}